package Aop;

import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.ThrowsAdvice;

import java.lang.reflect.Method;

/**
 * @Fou Jack of all trades and master of none.
 * @date 2022/1/5 14:19
 */
public class AOPInterceptor implements AfterReturningAdvice, MethodBeforeAdvice, ThrowsAdvice {

    @Override
    public void afterReturning(Object value, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("方法"+method.getName()+"运行结束，返回值为"+value);
    }

    @Override
    public void before(Method method, Object[] objects, Object instance) throws Throwable {
        System.out.println("执行MethodBeforeAdvice，即将执行的方法："+method.getName());
        if (instance instanceof IAOPServicesImpl){
            String
                    description = ((IAOPServicesImpl)instance).getDescription();
            if (description == null){
                throw new NullPointerException("description属性不能为空!");
            }
        }
    }

    public void afterThrowing(Exception ex){
        System.out.println("抛出了异常："+ex.getMessage());
    }
    public void afterThrowing(Method method,Object[] args,Object target, Exception ex){
        System.out.println("方法"+method.getName()+"抛出了异常："+ex.getMessage());
    }
}
